package com.lottery.material.controller.portal.transaction;

import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.jedis.JedisClient;
import com.lottery.material.resolver.CustomException;
import com.lottery.material.service.portal.transaction.TransactionRecordService;
import com.lottery.material.service.portal.transaction.TransactionTypeDictService;
import com.lottery.material.service.portal.user.PortalUserInfoService;
import com.lottery.material.util.ParameterUtil;
import com.lottery.material.util.ResponseResult;
import com.lottery.material.vo.portal.transaction.TradeRecordStatisticsVo;
import com.lottery.material.vo.portal.transaction.TransactionTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "交易接口", tags = { "交易接口" })
@Controller
@RequestMapping("/portal/transaction")
@Slf4j
public class TransactionController extends BaseController {

    @Autowired
    TransactionTypeDictService transactionTypeDictService;

    @Autowired
    TransactionRecordService transactionRecordService;

    @Autowired
    private PortalUserInfoService portalUserInfoService;

    @Autowired
    @Qualifier("jedisClientCluster")
    private JedisClient jedisClient;

    @ApiOperation(value="获取交易类型列表接口", notes="获取交易类型列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户唯一标识（登录的用户必传）", required = true, dataType = "String"),
            @ApiImplicitParam(name = "accessToken", value = "访问令牌（登录的用户必传）", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/getTransactionType" ,method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult getTransactionType(HttpServletRequest request) {

        Map<String, String> reqParamMap = new HashMap<>();
        //解析参数
        if (ParameterUtil.analyticParameter(request, reqParamMap)) {
            throw new CustomException(ResultEnum.PARSE_EXCEPTION);
        }

        //非空判断
        ResponseResult respDate = validateEmpty(reqParamMap);
        if (null != respDate) {
            return respDate;
        }

        ParameterUtil.checkLogin(reqParamMap, jedisClient, portalUserInfoService);

        ResponseResult x = ParameterUtil.checkSign(reqParamMap);
        if (x != null) return x;

        List<TransactionTypeVo> allTransactionTypeVo = transactionTypeDictService.findAllTransactionTypeVo();
        return ResponseResult.buildSuccess(allTransactionTypeVo);
    }


    @ApiOperation(value="交易记录接口", notes="交易记录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户唯一标识（登录的用户必传）", required = true, dataType = "String"),
            @ApiImplicitParam(name = "accessToken", value = "访问令牌（登录的用户必传）", required = true, dataType = "String"),
            @ApiImplicitParam(name = "timeType", value = "时间类型（1：当天；2：近一周；3：近一月；4：近三月）", required = true, dataType = "String"),
            @ApiImplicitParam(name = "transactionTypeCode", value = "交易类型编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "page", value = "用于分页（从1开始）", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/getTradeRecord" ,method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult getTradeRecord(HttpServletRequest request) {

        Map<String, String> reqParamMap = new HashMap<>();
        //解析参数
        if (ParameterUtil.analyticParameter(request, reqParamMap)) {
            throw new CustomException(ResultEnum.PARSE_EXCEPTION);
        }

        //非空判断
        ResponseResult respDate = validateEmpty(reqParamMap);
        if (null != respDate) {
            return respDate;
        }

        ParameterUtil.checkLogin(reqParamMap, jedisClient, portalUserInfoService);

        ResponseResult x = ParameterUtil.checkSign(reqParamMap);
        if (x != null) return x;


        String timeType = reqParamMap.get("timeType");
        if (StringUtils.isBlank(timeType)) {
            return ResponseResult.buildResult(ResultEnum.PARAMETER_BLANK_ERROR.getCode(), String.format(ResultEnum.PARAMETER_BLANK_ERROR.getMessage(),"timeType"));
        }
        String transactionTypeCode = reqParamMap.get("transactionTypeCode");
        if (StringUtils.isBlank(transactionTypeCode)) {
            return ResponseResult.buildResult(ResultEnum.PARAMETER_BLANK_ERROR.getCode(), String.format(ResultEnum.PARAMETER_BLANK_ERROR.getMessage(),"transactionTypeCode"));
        }
        String page = reqParamMap.get("page");
        if (StringUtils.isBlank(page)) {
            return ResponseResult.buildResult(ResultEnum.PARAMETER_BLANK_ERROR.getCode(), String.format(ResultEnum.PARAMETER_BLANK_ERROR.getMessage(),"page"));
        }
        TradeRecordStatisticsVo tradeRecordStatistics = transactionRecordService.findTradeRecordStatistics(reqParamMap);

        return ResponseResult.buildSuccess(tradeRecordStatistics);
    }

}
